<template>
  <el-container class="home_class">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>东平大学</span>
      </div>
      <el-button @click="logout" type="info">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <!-- 折叠条 -->
        <div class="toggle-button" @click="toggleC">|||</div>
        <!-- 折叠结构 -->
        <el-menu
          background-color="#090A0A"
          text-color="#fff"
          active-text-color="purple"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i :class="icons[item.id]"></i>
              <!-- 一级菜单名字 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="sevaSataus('/' + subitem.path)"
            >
              <template slot="title">
                <!-- 一级菜单图标 -->
                <i class="el-icon-s-grid"></i>
                <!-- 一级菜单名字 -->
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体  -->
      <el-main>
        

       
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "myHome",
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      icons: {
        125: "el-icon-user-solid",
        103: "el-icon-s-grid",
        101: "el-icon-goods",
        102: "el-icon-document-copy",
        145: "el-icon-s-platform",
      },
      iscollapse: false,
      // 被激活的链接地址
      activePath: "",
    };
  },
  // 生命周期函数 开始就调用
  created() {
    this.getMenuList();
    this.activePath = sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取说有的菜单
    async getMenuList() {
      const { data: res } = await this.$axios.get("menus");
      // 判断 如果请求不成功 就会通过饿了么组件返回错误
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      // console.log(res);
    },
    toggleC() {
      this.iscollapse = !this.iscollapse;
    },
    // 保存链接的激活状态
    sevaSataus(activePath) {
      sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="less" scoped>
.home_class {
  height: 100%;
}
// 标题
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  border-radius: 0 0 10% 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  background-color: #151617;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
  img {
    width: 17%;
  }
}
// 侧边栏
.el-aside {
  background-color: #090a0a;
  .em-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #ffffff;
}
// 折叠部分
.toggle-button {
  background-color: #151516;
  font-size: 10px;
  width: 99%;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>